package com.jse.util.concurrent.new1.synchronizer;

import org.junit.Test;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author : zenghua
 * @version : v0.1 CountDownLatchTest2.java
 * @since : 2018/3/28 10:47
 */
public class CountDownLatchTest2 {

	/**
	 * 查询写文件分离
	 */
	@Test
	public void testMulti() throws Exception {
		ExecutorService executorService = Executors.newFixedThreadPool(10);
		CountDownLatch latch = new CountDownLatch(10);
		for (int i = 0; i < 10; i++) {
			//查询数据
			List<String> dataList = Arrays.asList(UUID.randomUUID().toString(), UUID.randomUUID().toString());
			FileTask fileTask = new FileTask(dataList, latch);
			//写入文件
			executorService.execute(fileTask);
			System.out.println(i);
		}

		latch.await();
		System.out.println("汇总执行压缩");
	}

	public void write(List<File> fileList) {

	}

	class FileTask implements Runnable {
		private List<String> dataList;
		private CountDownLatch latch;

		public FileTask() {}

		public FileTask(List<String> dataList, CountDownLatch latch) {
			this.dataList = dataList;
			this.latch = latch;
		}

		@Override
		public void run() {
			try {
				System.out.println(1/0);
				Thread.sleep(6000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			} finally {
				//如果有异常这里也会执行（使用锁一定要在finally里面释放锁）
				latch.countDown();
			}
		}
	}
}
